package cn.com.nes.site.action.monitor;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.service.monitor.TaskService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller("task")
@RequestMapping("/task")
public class TaskAction {

    @Resource
    private TaskService service;


    /**
     * 查询控制任务
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getControlTasks")
    @ResponseBody
    public PageData getControlTasks(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getControlTasks(reqStatus.getReq());
        return res;
    }

    /**
     * 新增控制任务
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(method = RequestMethod.POST, value = "/addControlTask")
    @ResponseBody
    public PageData addControlTask(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.addControlTask(reqStatus.getReq());
        return res;
    }

    /**
     * 删除控制任务   可批量
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/deleteControlTask")
    @ResponseBody
    public PageData deleteControlTask(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.deleteControlTask(reqStatus.getReq());
        return res;
    }

}
